package com.pt25.service.sys.domain.admin.service.impl;

import com.pt25.base.dep.redis.RedisService;
import com.pt25.base.exception.BizException;
import com.pt25.base.model.common.constants.RedisConstant;
import com.pt25.base.util.CollectionUtil;
import com.pt25.base.util.Md5Util;
import com.pt25.base.util.PageUtil;
import com.pt25.base.util.StrUtil;
import com.pt25.service.sys.domain.admin.ddo.AdminUser;
import com.pt25.service.sys.domain.admin.factory.AdminFactory;
import com.pt25.service.sys.domain.admin.service.AdminUserService;
import com.pt25.service.sys.repository.mysql.persistence.AdminUserRepo;
import com.pt25.service.sys.repository.mysql.persistence.AdminUserRoleRepo;
import com.pt25.service.sys.repository.mysql.po.AdminUserPo;
import com.pt25.service.sys.repository.mysql.po.AdminUserRolePo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    private static String passwordMD5SecretKey = "@#$%pt";

    @Resource
    private RedisService redisService;

    @Resource
    private AdminUserRepo adminUserRepo;

    @Resource
    private AdminUserRoleRepo adminUserRoleRepo;

    @Resource
    private AdminFactory adminFactory;

    @Transactional
    @Override
    public void addAdminUser(AdminUser adminUser) {
        if (null == adminUserRepo.getAdminUserByAdmin(adminUser.getAdmin())) {
            AdminUserPo adminUserPO = adminFactory.adminUser2AdminUserPO(adminUser);
            adminUserPO.setId(redisService.genAutoIncrID(RedisConstant.CLUSTER1, AdminUserPo.key));
            adminUserPO.setPassword(Md5Util.md5(adminUser.getPassword() + passwordMD5SecretKey));
            adminUserRepo.save(adminUserPO);
            //添加用户角色数据
            adminUser.setId(adminUserPO.getId());
            addUserRole(adminUser);
            return;
        }
        throw new BizException("用户名:" + adminUser.getAdmin() + "已经存在！");
    }

    @Override
    public void delAdminUser(Long id) {
        //删除用户
        adminUserRepo.removeById(id);
        //删除用户角色
        adminUserRoleRepo.delUserRoleByUserId(id);
    }

    @Override
    public void updateAdminUser(AdminUser adminUser) {
        AdminUser adminUserByAdmin = adminUserRepo.getAdminUserByAdmin(adminUser.getAdmin());
        if (null == adminUserByAdmin || adminUserByAdmin.getId().equals(adminUser.getId())) {
            AdminUserPo adminUserPO = adminFactory.adminUser2AdminUserPO(adminUser);
            adminUserPO.setAdmin(StrUtil.isNotBlank(adminUser.getAdmin()) ? adminUser.getAdmin() : null);
            adminUserPO.setPassword(StrUtil.isNotBlank(adminUser.getPassword()) ? Md5Util.md5(adminUser.getPassword() + passwordMD5SecretKey) : null);
            adminUserRepo.updateById(adminUserPO);
            //删除用户角色
            adminUserRoleRepo.delUserRoleByUserId(adminUser.getId());
            //添加用户角色
            addUserRole(adminUser);
            return;
        }
        throw new BizException("用户名称:" + adminUser.getAdmin() + "已经存在！");
    }

    @Override
    public AdminUser getAdminUser(Long id) {
        return adminUserRepo.getAdminUser(id);
    }

    @Override
    public AdminUser getLoginAdminUser(AdminUser adminUser) {
        AdminUserPo adminUserPO = adminFactory.adminUser2AdminUserPO(adminUser);
        adminUserPO.setPassword(Md5Util.md5(adminUser.getPassword() + passwordMD5SecretKey));
        return adminUserRepo.getLoginAdminUser(adminUserPO);
    }

    @Override
    public PageUtil.PageVo pageAdminUser(AdminUser adminUser) {
        return adminUserRepo.pageAdminUser(adminFactory.adminUser2AdminUserPO(adminUser));
    }

    private void addUserRole(AdminUser adminUser) {
        if (CollectionUtil.isEmpty(adminUser.getRoleIds())) {
            return;
        }
        List<AdminUserRolePo> adminUserRolePoList = new ArrayList<>();
        for (long roleId : adminUser.getRoleIds()) {
            AdminUserRolePo adminUserRolePO = AdminUserRolePo.builder().roleId(roleId).userId(adminUser.getId()).build();
            adminUserRolePO.setId(redisService.genAutoIncrID(RedisConstant.CLUSTER1, AdminUserRolePo.key));
            adminUserRolePoList.add(adminUserRolePO);
        }
        adminUserRoleRepo.saveBatch(adminUserRolePoList);
    }
}
